<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>进货统计-按商品</title>
<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
<script>
   var getClassUrl = "<%=request.getContextPath()%>/product/getAllPoductClassJson.action";
   $(function(){
	   $('.icon-reload').click(
				function (){ 
					$('#ocTree').tree({url:'<%=request.getContextPath()%>/product/getAllPoductClassJson.action'});
					$('#ocTree').tree('reload')
				}
			);
			$(".icon-reload").css("cursor","pointer");
			
		$('#ocTree').tree({
			animate: true,
			url: getClassUrl,
			onBeforeExpand:function(node){
				$('#ocTree').tree("options").url=getClassUrl + "?pc.id=" + node.id;
			},
			onClick:function(node){
				var node = $('#ocTree').tree('getSelected');
				$('#orgs').datagrid('reload',{'searchVoucherCondition.pcId':node.id});
				$('#pcId').val(node.id);
				$('#ocTree').tree('expand',node.target);
			},
			onContextMenu: function(e, node){
				e.preventDefault();
				$('#ocTree').tree('select', node.target);
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
			}
			
		});
		// 右侧表单内容
		$('#orgs').datagrid({
				nowrap: false,
				striped: true,
				collapsible:true,
				fit : true,
				fitColumns : true,	
				url:'<%=request.getContextPath()%>/searchcenter/searchPurchaseByProduct.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				title:'统计结果',
				columns:[[
					{field:'id',title:'id',width:200,hidden:'true'},
					{field:'name',title:'名称',width:200},
					{field:'proClassName',title:'商品分类',width:100},
					{field:'spec',title:'规格',width:100},
					/* {field:'unit',title:'单位',width:120}, */
					{field:'amount',title:'进货总数量',width:100,align:'right',sortable:true},
					{field:'averagePrice',title:'进货均价(元)',width:100,align:'right',sortable:true,
						formatter:function(value,row,index){
							return row.averagePrice.toFixed(2);				
						}
					},
					{field:'totalSubTotal',title:'进货总金额(元)',width:100,align:'right',sortable:true,
						formatter:function(value,row,index){
							return row.totalSubTotal.toFixed(2);				
						}
					},
					{field:'returnAmount',title:'退货总数量',width:100,align:'right',sortable:true},
					{field:'averageReturnPrice',title:'退货均价(元)',width:150,align:'right',sortable:true,
						formatter:function(value,row,index){
							return row.averageReturnPrice.toFixed(2);				
						}
					},
					{field:'returnPrice',title:'退货总金额(元)',width:100,align:'right',sortable:true,
						formatter:function(value,row,index){
							return row.returnPrice.toFixed(2);
						}
					},
					{field:'action',title:'操作',width:50,
						  formatter:function(value,row,index){
			                  return '<a href="#" onclick="detail(' + row.id + ',' + '\'' + row.name + '\'' + ')">明细账</a>'
                		 }
					}
				]],
				pageSize:15,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
			});
   });
	function detail(id,name){
		$("input[name=voucherType]:eq(2)").attr("checked",'checked'); 
		$("#proId").val(id);
		if(name.length > 25){
			name=name.substr(0,25) + "...";
		}
		$("#proName").html('<b>' +  name + '</b>');
		var title = "进货明细查询(按商品)";
		var stime = $("input[name=stime]").val();
   		var etime = $("input[name=etime]").val();
		// 详细内容
   		$('#detailDlg').dialog('open').dialog('setTitle',title); 
		$('#detailTable').datagrid({
			fit:true,
			nowrap: false,
			striped: true,
			collapsible:true,
			url:'<%=request.getContextPath()%>/searchcenter/searchVoucherByProId.action',
			sortName: 'code',
			sortOrder: 'desc',
			remoteSort: false,
			idField:'code',
			singleSelect:true,
			queryParams:{'searchVoucherCondition.proId':id,'searchVoucherCondition.stime':stime,'searchVoucherCondition.etime':etime},
			columns:[[
				{field:'date',title:'单据日期',width:80,sortable:true},
				{field:'no',title:'单据编号',width:90},
				/* {field:'voucherType',title:'单据类型',width:100}, */
				{field:'org',title:'往来单位',width:80},
				{field:'warehouse',title:'仓库',width:80},
				{field:'amount',title:'数量',width:60,align:'right',sortable:true,
					 formatter:function(value,row,index){
					    if(row.voucherType=="returnwarehouse"){
					 		return "<font color='red'>-" + row.amount + "</font>";
					 	}
					 	return row.amount;
					 }
				},
				{field:'subtotal',title:'小计(元)',width:60,align:'right',sortable:true,
					formatter:function(value,row,index){
					    if(row.voucherType=="returnwarehouse"){
					 		return "<font color='red'>-" + row.subtotal.toFixed(2) + "</font>";
					 	}
					 	return row.subtotal.toFixed(2);
					 }
				},
				{field:'action',title:'操作',width:30,
					 formatter:function(value,row,index){
					   	   var s;
					 	   if(row.voucherType=="warehouse"){
					 	   		s = '<a href="javascript:parent.addTabDirect(\'进货入库单\',\'<%=request.getContextPath()%>/voucher/toAddWareHouseVoucher.action?id=' + row.id + '\',\'icon-page-white\')">详细</a>';		
					 	   }
					 	   if(row.voucherType=="returnwarehouse"){
		                   		s = '<a href="javascript:parent.addTabDirect(\'进货退货单\',\'<%=request.getContextPath()%>/voucher/toAddReturnWareHouseVoucher.action?id=' + row.id + '\',\'icon-page-white\')">详细</a>';
		                   }
                  		   return s;
                	 }				
				}
			]],
			pageSize:10,  
            pageList:[5,10,15,20],  
            fitColumns: true,  
			pagination:true,
			rownumbers:true
		});
		$('#detailTable').datagrid({pageNumber:1});
	}
	function searchVoucher(){
		var node = $('#ocTree').tree('getSelected');
		var pcId;
		if(node!=null){
			pcId = node.id;
		}
		var searchProName = $("#searchProName").val();
		if($.trim(searchProName) != ""){
			pcId = "";
		}
		var stime = $("input[name=stime]").val();
   		var etime = $("input[name=etime]").val();
   		$("#sshowtime").html("<b>" + stime + "</b>");
   		$("#eshowtime").html("<b>" + etime + "</b>");
		$('#orgs').datagrid('reload',{'searchVoucherCondition.pcId':pcId,'searchVoucherCondition.stime':stime,'searchVoucherCondition.etime':etime,'searchVoucherCondition.searchProName':searchProName});
		$('#orgs').datagrid({pageNumber:1});
	}
	function searchClear(){
		location.reload()
	}
	
	var saveOrUpdateUrl;
	function exportExcel(){ 
		saveOrUpdateUrl = "<%=request.getContextPath()%>/searchcenter/exportExcelProducts.action";
	    $('#searchForm').form('submit',{  
	        url:saveOrUpdateUrl,  
	        onSubmit: function(){  
	        	var stime = $("#stime").val();
	        	var etime = $("#etime").val();
	        	var pcId = $("#pcId").val();
	        	var searchProName = $("#searchProName").val();
	        	var voucherTypeStr = $("#voucherTypeStr").val();
	            return $(this).form('validate');  
	        },  
	        success: function(result){  
	            var result = eval('('+result+')');
	            if (result==true){
	            	$.messager.show({title:"提示信息:", msg:"导出成功！"});
	            } else {  
	                $.messager.show({title:"提示信息:", msg:"导出失败！"});  
	            }  
	        }  
	    });  
	}

	//进货统计-按商品-详细
	function exportWareHouseDetail(){ 
		var proId=$("#proId").val();
		var voucherType = 'warehouse,returnwarehouse';
		$('input[name=voucherType]').each(function(){
			if($(this).attr('checked') == 'checked'){
				voucherType = $(this).val();
			}
		});
		saveOrUpdateUrl = "<%=request.getContextPath()%>/searchcenter/exportWareHouseDetail.action?pid="+proId+"&queryVoucherType="+voucherType;
	    window.location.href = saveOrUpdateUrl;
	}
	function reloadVouchers(voucherType){
		var queryParams = $("#detailTable").datagrid('options').queryParams;
		queryParams.queryVoucherType = voucherType;
		$('#detailTable').datagrid('options').queryParams=queryParams;
		$('#detailTable').datagrid({pageNumber:1});
		$('#detailTable').datagrid('reload');
	}
</script>
</head>
<body class="easyui-layout" >
<div region="west" iconCls="icon-reload" title="商品分类" split="true" style="width:180px;">
		<ul id="ocTree"></ul>
</div>
<!-- 条件查询table -->
<div region="center" title="进货统计-按商品" split="true">
	<div class="easyui-layout" data-options="fit:true">
		<div style="height:130px" data-options="region:'north',border:false">
			<div id="tb" class="datagrid-toolbar">  
				    <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="javascript:searchVoucher()">查询</a>  
				    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="javascript:searchClear()">重置</a>  
				    <a href="#" class="easyui-linkbutton" iconCls="icon-excel" plain="true" onclick="javascript:exportExcel()">导出</a>
			</div>
			<center>
			<form id="searchForm" method="post">
			<table style="font-size:12px;padding:15px" border="0">
				<input type="hidden" id="pcId" name="pcId" value="" />
				<tr>
					<td>
						商品名称 
					</td>
					<td align="left">
						<input type="text" name="searchProName" id="searchProName" title="查询所有商品" value="" />
					</td>
				</tr>
				<tr>
					<td align="right">
						统计日期
					</td>
					<td>
						<input class="easyui-datebox" name="stime" id="stime" style="width:100px" readonly="readonly" />
						至
						<input class="easyui-datebox" name="etime" id="etime" style="width:100px" readonly="readonly" />
						<input type="hidden" name="voucherTypeStr" id="voucherTypeStr" value="warehouse" />
					</td>
				</tr>
			</table>
			</form>
			</center>
		</div>
		<div data-options="region:'center',border:false">
			<table id="orgs"></table>
		</div>
	</div>
</div>
<div id="detailDlg" class="easyui-dialog"  closed="true" style="width:800px;height:380px;"  resizable="true">
	<div class="easyui-layout" data-options="fit:true">
		<div style="height:31px" data-options="region:'north',border:false">
			<div id="tb" class="datagrid-toolbar">
				<div style="float:left">
					<input type="radio" id="voucherType" name="voucherType" onclick="reloadVouchers(this.value)" value="warehouse" />进货单
					<input type="radio" id="voucherType" name="voucherType" onclick="reloadVouchers(this.value)" value="returnwarehouse"/>退货单
					<input type="radio" id="voucherType" name="voucherType" onclick="reloadVouchers(this.value)" value="warehouse,returnwarehouse" checked="checked"/>全部&nbsp;
				</div>
				<div class="datagrid-btn-separator"></div>
				<input type="hidden" id="proId" name="proId" value="" />
				<div style="float:left;line-height:25px">
					&nbsp;商品名称:<span id="proName" name="proName"></span>&nbsp;
				</div>
				<div class="datagrid-btn-separator"></div>
				<div style="float:left">
			    	<a href="#" class="easyui-linkbutton" iconCls="icon-excel" plain="true" onclick="javascript:exportWareHouseDetail()">导出</a>
			    </div>
				<div class="datagrid-btn-separator"></div>
				<div style="float:left;line-height:25px">
				&nbsp;统计日期：<span id="sshowtime"><b>无</b></span> 至 <span id="eshowtime"><b>无</b></span>
				</div>
			</div>
		</div>
		<div data-options="region:'center',border:false">
	    	<table id="detailTable"></table>
	    </div>
    </div>
</div>
</body>
</html>